{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import librosa\n",
    "import librosa.display\n",
    "import IPython.display as ipd\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "TRAIN_PATH = '../../dataset/trainset/'\n",
    "labels = [ 'down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_names = []\n",
    "\n",
    "path = TRAIN_PATH + labels[0] + '/'\n",
    "pathname = Path(path)\n",
    "\n",
    "for cp in pathname.iterdir():\n",
    "    term = Path(cp)\n",
    "    file_names.append(term.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "fs = 16e3; # Known sample rate of the data set.\n",
    "\n",
    "segmentDuration = 1\n",
    "frameDuration = 0.025\n",
    "hopDuration = 0.010"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "segmentSamples = round(segmentDuration*fs)\n",
    "frameSamples = round(frameDuration*fs)\n",
    "hopSamples = round(hopDuration*fs)\n",
    "overlapSamples = frameSamples - hopSamples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16000 400 160 240\n"
     ]
    }
   ],
   "source": [
    "print(segmentSamples, frameSamples, hopSamples, overlapSamples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "FFTLength = 512\n",
    "numBands = 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pad_wave(y):\n",
    "    numSamples  = y.shape[0]\n",
    "    numToPadFront = np.floor( (segmentSamples - numSamples)/2 );\n",
    "    numToPadBack = np.ceil( (segmentSamples - numSamples)/2 );\n",
    "\n",
    "    numToPadFront = int(numToPadFront)\n",
    "    numToPadBack = int(numToPadBack)\n",
    "\n",
    "    num_pad = (numToPadFront, numToPadBack)\n",
    "    y_pad = np.pad(y, num_pad, 'constant')\n",
    "    \n",
    "    return y_pad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_mfcc(y):\n",
    "    mfcc = librosa.feature.mfcc(y=y, sr=fs, n_mfcc=numBands, n_fft=FFTLength, hop_length=hopSamples, win_length=frameSamples )\n",
    "    return mfcc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_mfcc(filename):\n",
    "    y, sr = librosa.load(filename,sr=None)\n",
    "    y_pad = pad_wave(y)\n",
    "    the_mfcc = extract_mfcc(y_pad)\n",
    "    return the_mfcc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_file = path + file_names[5]\n",
    "y, sr = librosa.load(test_file,sr=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pad = pad_wave(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "the_mfcc = extract_mfcc(y_pad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 101)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "the_mfcc.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "shapes=[]\n",
    "for i in range(100):\n",
    "    test_file = path + file_names[i]\n",
    "    the_mfcc = get_mfcc(test_file)\n",
    "    shapes.append(the_mfcc.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2.])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append(a,(1,2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
